﻿using JingBaiHui.Common;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DianPingDataGrab.Provider
{
    public class DianPingProvider
    {
        public DataTable DtResult = new DataTable();
        public DianPingProvider()
        {
            //名称，联系人，电话，地址，区域
            DataTable dt = DtResult;
            dt.Columns.Add("区域");
            dt.Columns.Add("名称");
            dt.Columns.Add("电话");
            dt.Columns.Add("地址");
            dt.Columns.Add("链接");

        }
        private void SetLog(TextBox txtLog, string msg)
        {
            txtLog.BeginInvoke((MethodInvoker)delegate ()
            {
                txtLog.Text = $"{msg}{Environment.NewLine}" + txtLog.Text;
            });
        }

        private DataTable GetMainTable()
        {
            //名称，联系人，电话，地址，区域
            DataTable dt = new DataTable();
            dt.Columns.Add("区域");
            dt.Columns.Add("名称");
            dt.Columns.Add("电话");
            dt.Columns.Add("地址");
            dt.Columns.Add("链接");
            return dt;
        }
        public void InitGridHeader(DataGridView grid)
        {
            DataTable dt = DtResult;
            grid.BeginInvoke((MethodInvoker)delegate ()
            {
                foreach (DataColumn item in dt.Columns)
                {
                    var column = new DataGridViewTextBoxColumn();
                    column.HeaderText = item.ColumnName;
                    grid.Columns.Add(column);
                }
            });
        }

        public void SearchData(DataGridView grid, TextBox txtLog)
        {
            DtResult.Rows.Clear();
            grid.BeginInvoke((MethodInvoker)delegate ()
            {
                grid.Rows.Clear();
            });
            SetLog(txtLog, "开始同步获取区域链接.....");
            var areaLinks = GetAreaLinks(txtLog);
            SetLog(txtLog, $"区域链接获取成功，共获取区域链接 {areaLinks.Count} 个");
            foreach (var item in areaLinks)
            {
                SearchArea(grid, txtLog, item.Key, item.Value);
            }
        }

        private void SearchArea(DataGridView grid, TextBox txtLog, string areaName, string areaLink)
        {
            string domianUrl = "http://www.dianping.com";

            int pageCount = 50;
            for (int i = 1; i <= pageCount; i++)
            {
                string areaPageUrl = areaLink + "p" + i;
                var html = HttpHelper.Request(areaPageUrl, null, RestSharp.Method.GET);
                if (string.IsNullOrWhiteSpace(html))
                    continue;

                var doc = new HtmlAgilityPack.HtmlDocument();
                doc.LoadHtml(html);
                var mainNode = doc.DocumentNode.SelectSingleNode("//*[@id=\"shop-all-list\"]/ul");
                if (mainNode == null)
                    continue;

                var liNodes = mainNode.ChildNodes.Where(t => t.Name.Equals("li")).ToList();
                for (int j = 0; j < liNodes.Count; j++)
                {
                    try
                    {
                        var href = liNodes[j].ChildNodes.FirstOrDefault(t => t.OuterHtml.Contains("class=\"pic\"")).ChildNodes.FirstOrDefault(t => t.Name.Equals("a")).Attributes["href"].Value;
                        var shopName = liNodes[j].ChildNodes[3].InnerText.Replace("\n", "").Replace("\r", "").Trim();
                        href = domianUrl + href;
                        SetLog(txtLog, $"详情页链接 {href} 获取成功！开始抓取详细内容...");
                        GrabDetailPage(grid, txtLog, href, areaName);
                    }
                    catch (Exception ex)
                    {
                        SetLog(txtLog, $"{areaName},链接：{areaPageUrl} 第{j}条的详情页解析失败！msg:{ex.Message}");
                    }
                }
            }
        }

        private void GrabDetailPage(DataGridView grid, TextBox txtLog, string url, string areaName)
        {
            var html = HttpHelper.Request(url, null, RestSharp.Method.GET);
            if (string.IsNullOrWhiteSpace(html))
                return;

            HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
            doc.LoadHtml(html);
            //*[@id="basic-info"]/h1
            var titleNode = doc.DocumentNode.SelectSingleNode("//*[@id=\"basic-info\"]/h1");
            var addressNode = doc.DocumentNode.SelectSingleNode("//*[@id=\"basic-info\"]/div[2]/span[2]");
            var phoneNode = doc.DocumentNode.SelectSingleNode("//*[@id=\"basic-info\"]/p/span[2]");

            DataRow dr = DtResult.NewRow();
            dr["区域"] = areaName;
            dr["名称"] = TrimOther(titleNode.FirstChild.InnerText);
            dr["电话"] = TrimOther(phoneNode.InnerText);
            dr["地址"] = TrimOther(addressNode.InnerText);
            dr["链接"] = url;
            DtResult.Rows.Add(dr);

            grid.BeginInvoke((MethodInvoker)delegate ()
            {
                grid.Rows.Insert(0, dr.ItemArray);
            });
        }

        private string TrimOther(string result)
        {
            return result.Replace("\n", string.Empty).Replace("\r", string.Empty).Trim();
        }
        private Dictionary<string, string> GetAreaLinks(TextBox txtLog)
        {
            var areaUrls = new Dictionary<string, string>();
            string domianUrl = "http://www.dianping.com";
            string url = "http://www.dianping.com/shopall/344/0#BDBlock";
            var html = HttpHelper.Request(url, null, RestSharp.Method.GET);
            HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
            doc.LoadHtml(html);
            var mainNode = doc.DocumentNode.SelectSingleNode("//*[@id=\"top\"]/div[6]/div/div[2]");
            var areaNodes = mainNode.ChildNodes.Where(t => t.InnerHtml.Contains("class=\"Bravia\"")).ToList();
            for (int i = 0; i < areaNodes.Count; i++)
            {
                var areaNode = areaNodes[i];
                var ddNode = areaNode.ChildNodes.FirstOrDefault(t => t.Name.Equals("dd"));
                if (ddNode != null)
                {
                    var ulNode = ddNode.ChildNodes.FirstOrDefault(t => t.Name.Equals("ul"));
                    if (ulNode != null)
                    {
                        var liNodes = ulNode.ChildNodes.Where(t => t.Name.Equals("li")).ToList();
                        for (int h = 0; h < liNodes.Count; h++)
                        {
                            var aNode = liNodes[h].ChildNodes.FirstOrDefault(t => t.Name.Equals("a"));
                            var href = domianUrl + aNode.Attributes["href"].Value;
                            var areaName = areaNode.ChildNodes[1].InnerText + "-" + aNode.InnerText;
                            //SetLog(txtLog, $"已获取区域：{areaName}，链接:{href}");
                            areaUrls.Add(areaName, href);
                        }
                    }
                }
            }
            return areaUrls;
        }
    }
}
